import tkinter as tk
from tkinter import filedialog, messagebox
import os
import json
import sys
from automation import NGReportAutomation
from gui import NGReportGUI
from login import show_login_window, load_config
import logging
from logging_config import setup_logging
from config_manager import ConfigManager


def main():
    # 初始化日志配置
    setup_logging()
    logger = logging.getLogger(__name__)
    # 加载配置文件
     # 获取当前脚本的目录
    current_dir = os.path.dirname(os.path.abspath(__file__))
    config_path = os.path.join(current_dir, 'config.json')
    config = load_config(config_path)
    if config is None:
        print("无法加载配置文件")
        sys.exit(1)
    
    enable_login = config.get('enable_login', True)
    # 获取配置
    config_manager = ConfigManager()
    config = config_manager.get_config()
    logger.info("当前配置: %s", config)
    if enable_login:
        # 显示登录窗口
        def on_login_success():
            root = tk.Tk()
            app = NGReportGUI(root)
            root.mainloop()
        
        show_login_window(on_login_success)
    else:
        # 直接进入主程序
        root = tk.Tk()
        app = NGReportGUI(root)
        root.mainloop()

if __name__ == "__main__":
    main()